#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDebug>
#include <QPoint>
//#include <QGraphicsOpacityEffect>
//#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //初始化动画
    animatedButton = new QPushButton("Animated Button", this);
    animatedButton->setStyleSheet("background-color: #4CAF50; border-radius: 5px;");
    animatedButton->setFixedSize(200,50);
    //设置初始位置
    originalPos=QPoint(50,100);
    animatedButton->move(originalPos);
    // 创建控制按钮
    QPushButton *LinearButton = new QPushButton("Linear", this);
    QPushButton *InQuadButton = new QPushButton("InQuad", this);
    QPushButton *OutElasticButton = new QPushButton("OutElastic", this);//弹性效果
    QPushButton *OutBounceButton = new QPushButton("OutBounce", this);//弹跳效果
    QPushButton *ResetButton =new QPushButton("Reset",this);
    QPushButton *FadeInButton =new QPushButton("FadeIn",this);
    QPushButton *FadeOutButton = new QPushButton("FadeOut",this);


    QString controlBtnStyle =
            "QPushButton {"
            "   background-color: #f1f1f1;"
            "   border: 1px solid #ddd;"
            "   padding: 8px;"
            "   min-width: 80px;"
            "}"
            "QPushButton:hover { background-color: #e7e7e7; }";

    LinearButton->setStyleSheet(controlBtnStyle);
    InQuadButton->setStyleSheet(controlBtnStyle);
    OutElasticButton->setStyleSheet(controlBtnStyle);
    OutBounceButton->setStyleSheet(controlBtnStyle);
    FadeInButton->setStyleSheet(controlBtnStyle);
    FadeOutButton->setStyleSheet(controlBtnStyle);
    ResetButton->setStyleSheet("backgroung-color: #f44336;""color: white;""QPushButton:hover { background-color: #d32f2f; }");

    QVBoxLayout *mainLayout = new QVBoxLayout();
    QHBoxLayout *controlLayout1 = new QHBoxLayout();
    controlLayout1->addWidget(LinearButton);
    controlLayout1->addWidget(InQuadButton);
    controlLayout1->addWidget(OutElasticButton);
    controlLayout1->addWidget(OutBounceButton);
    QHBoxLayout *controlLayout2 = new QHBoxLayout();
    controlLayout2->addWidget(FadeInButton);
    controlLayout2->addWidget(FadeOutButton);
    controlLayout2->addWidget(ResetButton);
    mainLayout->addWidget(animatedButton,0,Qt::AlignLeft);
    mainLayout->addSpacing(50);
    mainLayout->addLayout(controlLayout1);
    mainLayout->addSpacing(30);
    mainLayout->addLayout(controlLayout2);

    QWidget *centralWidget =new QWidget(this);
    centralWidget->setLayout(mainLayout);
    setCentralWidget(centralWidget);
    //
    animation = new QPropertyAnimation(animatedButton,"pos",this);//动画属性
    animation->setDuration(2000);
    animation->setStartValue(originalPos);
    animation->setEndValue(QPoint(350, 100));

    //透明度动画 (新增)
    opacityEffect = new QGraphicsOpacityEffect(this);
    animatedButton->setGraphicsEffect(opacityEffect);
    opacityAnimation = new QPropertyAnimation(opacityEffect, "opacity", this);
    opacityAnimation->setDuration(1000);

    connect(LinearButton,&QPushButton::clicked,this,&MainWindow::onLinearClicked);
    connect(InQuadButton,&QPushButton::clicked,this,&MainWindow::onInQuadClicked);
    connect(OutElasticButton,&QPushButton::clicked,this,&MainWindow::onOutElasticClicked);
    connect(OutBounceButton,&QPushButton::clicked,this,&MainWindow::onOutBounceClicked);
    connect(FadeInButton,&QPushButton::clicked,this,&MainWindow::onFadeInClicked);
    connect(FadeOutButton,&QPushButton::clicked,this,&MainWindow::onFadeOutClicked);
    connect(ResetButton,&QPushButton::clicked,this,&MainWindow::resetAnimation);

}

MainWindow::~MainWindow()
{
    delete ui;
    delete animation;
    delete opacityAnimation;
    // opacityEffect会被自动删除，因为它是animatedButton的child
}

void MainWindow::onLinearClicked()
{
    if(animation->state() == QAbstractAnimation::Running) {
            animation->stop();
        }
    animation->setEasingCurve(QEasingCurve::Linear);
    animation->start();
}

void MainWindow::onInQuadClicked()
{
    if(animation->state() == QAbstractAnimation::Running) {
            animation->stop();
        }
    animation->setEasingCurve(QEasingCurve::InQuad);
    animation->start();
}
void MainWindow::onOutElasticClicked()
{
    if(animation->state() == QAbstractAnimation::Running) {
            animation->stop();
        }
    animation->setEasingCurve(QEasingCurve::OutElastic);
    animation->start();
}
void MainWindow::onOutBounceClicked()
{
    if(animation->state() == QAbstractAnimation::Running) {
            animation->stop();
        }
    animation->setEasingCurve(QEasingCurve::OutBounce);
    animation->start();
}
void MainWindow::onFadeInClicked()
{
    qDebug() << "FadeIn button clicked";
    if(opacityAnimation->state() == QAbstractAnimation::Running)
    {
        opacityAnimation->stop();
    }
    qDebug() << "Current opacity:" << opacityEffect->opacity();
    opacityAnimation->setStartValue(0.2);
    opacityAnimation->setEndValue(1.0);
    opacityAnimation->start();
}
void MainWindow::onFadeOutClicked()
{
    if(opacityAnimation->state() == QAbstractAnimation::Running)
    {
        opacityAnimation->stop();
    }
    opacityAnimation->setStartValue(1.0);
    opacityAnimation->setEndValue(0.2);
    opacityAnimation->start();
}
void MainWindow::resetAnimation()
{
    animation->stop();
    opacityAnimation->stop(); // 新增
    animatedButton->move(originalPos);
    opacityEffect->setOpacity(originalOpacity);
}

